package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Vote;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import util.KeyGen;
import util.MyJson;
import util.ParameterCheck;
import dao.CommentDao;
import dao.ItemDao;
import dao.VoteDao;

/**
 * Servlet implementation class AgreeView
 */
public class AgreeView extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(AgreeView.class);

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		request.setCharacterEncoding("utf-8");
		String msg = "";
		String result = "";
		String key = "";
		Cookie[] cookies = request.getCookies();
		for (int i = 0; i < cookies.length; i++) {
			if ("user".equals(cookies[i].getName())) {
				key = cookies[i].getValue();
			}
		}
		
		if (!KeyGen.cookieValid(key)) {
			result="0";
			msg="您尚未登录，请登录后再继续操作，谢谢！";
		} 
		else{
		String commentid = request.getParameter("commentid");
		String iid = request.getParameter("itemid");

		// log.debug("userid:" + userid + ";itemid:" + commentid + ";commentid:"
		// + commentid);

		
		String userid = KeyGen.getId(key);
		if (ParameterCheck.isEmpty(userid, commentid)
				&& !commentid.contains("_")) {
			msg = "数据错误，请稍后再试！";
			result="-1";
		} else {
			String comment_uid = commentid.split("_")[1];
			commentid = commentid.split("_")[0];
//			System.out.println(userid+"   "+comment_uid);
			
			if (userid.equals(comment_uid)) {//给自己的评论投票 无效
				msg="您不能投票给自己发表的评论！";
				result="-2";
			} else {
				int uid = Integer.parseInt(userid);
				int cid = Integer.parseInt(commentid);
				int itemid = KeyGen.convertCode2Id(iid);

				
				Vote vo = new Vote();
				vo.setUid(uid);
				vo.setItemid(itemid);
				vo.setCommentid(cid);
				int n = new VoteDao().insert(vo);
				
				if (n>0){
					new ItemDao().updateHot(itemid, 1);
					CommentDao cd = new CommentDao();
					cd.updateVote(cid);
					msg="投票成功！";
				}
				else if (-3==n){
					msg="您已经对该评论投过票啦！";
				}
				result=String.valueOf(n);
			}
		} 
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("msg", msg);
		jsonObject.put("res", result);
		pw.print(jsonObject.toString());
		pw.flush();
		pw.close();
	}
}
